package com.app;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.app.bean.User;
import com.app.presenter.UserPresenter;
import com.app.presenter.UserPresenterImpl;
import com.app.viewImpl.MainView;

public class MainActivity extends AppCompatActivity implements MainView {

    private EditText name_et, pass_et ;
    private Button mBtnLogin ;
    private ProgressBar mPbLoading;

    private UserPresenter userPresenter = new UserPresenterImpl( this ) ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        name_et = (EditText) findViewById(R.id.mEtUsername);
        pass_et = (EditText) findViewById(R.id.mEtPassword);
        mPbLoading = (ProgressBar) findViewById( R.id.mPbLoading );

        mBtnLogin = (Button) findViewById(R.id.mBtnLogin);

        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = name_et.getText().toString();
                String pass = pass_et.getText().toString();
                userPresenter.loadData(name, pass);
            }
        });

    }

    @Override
    public void start() {
        mPbLoading.setVisibility(View.VISIBLE);
    }

    @Override
    public void end() {
        mPbLoading.setVisibility(View.GONE);
    }

    @Override
    public void loginSuccess(Object result) {
        User user = (User) result;
        Toast.makeText(MainActivity.this, "login success " + user.getName() + " " + user.getPass() , Toast.LENGTH_SHORT).show();
    }

    @Override
    public void loginFail() {
        Toast.makeText(MainActivity.this, "login fail ", Toast.LENGTH_SHORT).show();
    }

}
